package com.xunyin.modules.mtservicefac.service.impl;


import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.xunyin.common.service.impl.BaseServiceImpl;
import com.xunyin.modules.mtservicefac.domain.MtServiceFac;
import com.xunyin.modules.mtservicefac.service.MtServiceFacService;
import com.xunyin.modules.mtservicefac.service.mapper.MtServiceFacMapper;
import com.xunyin.pagination.PaginationRequest;
import com.xunyin.pagination.PaginationResponse;
import lombok.AllArgsConstructor;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.PageImpl;
import org.springframework.data.domain.PageRequest;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import java.util.Date;
import java.util.List;

/**
 * 服务设施业务实现类
 * <p>
 * Created by xunyinyunshang <3594419785@qq.com>
 * CopyRight 讯音云商 https://www.tonetn.com
 */
@Service
@AllArgsConstructor
@Transactional(propagation = Propagation.SUPPORTS, readOnly = true, rollbackFor = Exception.class)
public class MtServiceFacImpl extends BaseServiceImpl<MtServiceFacMapper, MtServiceFac> implements MtServiceFacService {

    @Autowired
    private MtServiceFacMapper mtServiceFacMapper;

    /**
     * 服务设施查询列表
     *
     * @param paginationRequest
     * @return
     */
    @Override
    public PaginationResponse<MtServiceFac> queryListByPagination(PaginationRequest paginationRequest) {
        Page<MtServiceFac> pageHelper = PageHelper.startPage(paginationRequest.getCurrentPage(), paginationRequest.getPageSize());
        LambdaQueryWrapper<MtServiceFac> lambdaQueryWrapper = Wrappers.lambdaQuery();
        lambdaQueryWrapper.eq(MtServiceFac::getIsDel, 0);
        lambdaQueryWrapper.orderByDesc(MtServiceFac::getId);
        List<MtServiceFac> dataList = mtServiceFacMapper.selectList(lambdaQueryWrapper);

        PageRequest pageRequest = PageRequest.of(paginationRequest.getCurrentPage(), paginationRequest.getPageSize());
        PageImpl pageImpl = new PageImpl(dataList, pageRequest, pageHelper.getTotal());
        PaginationResponse<MtServiceFac> paginationResponse = new PaginationResponse(pageImpl, MtServiceFac.class);
        paginationResponse.setTotalPages(pageHelper.getPages());
        paginationResponse.setTotalElements(pageHelper.getTotal());
        paginationResponse.setContent(dataList);

        return paginationResponse;
    }

    /**
     * 保存服务设施信息
     *
     * @param mtServiceFac
     */
    @Override
    @Transactional
    public MtServiceFac saveMtServiceFac(MtServiceFac mtServiceFac) {
        mtServiceFac.setUpdateTime(new Date());
        if (mtServiceFac.getId() == null || mtServiceFac.getId() <= 0) {
            mtServiceFac.setCreateTime(new Date());
            this.save(mtServiceFac);
        } else {
            Integer id = mtServiceFac.getId();
            MtServiceFac mtServiceFacTemp = mtServiceFacMapper.selectById(id);
            this.updateById(mtServiceFacTemp);
        }
        return mtServiceFac;
    }

    /**
     * 根据ID获取服务设施信息
     *
     * @param id 服务设施id
     */
    @Override
    public MtServiceFac queryMtServiceFacById(Integer id) {
        return mtServiceFacMapper.selectById(id);
    }

    /**
     * 更改状态
     *
     * @param id
     */
    @Override
    public Integer updateStatus(Integer id, Integer status) {
        MtServiceFac mtServiceFac = mtServiceFacMapper.selectById(id);
        if (mtServiceFac != null) {
            mtServiceFac.setIsDel(1);
            mtServiceFac.setUpdateTime(new Date());
            mtServiceFacMapper.updateById(mtServiceFac);

        } else {
            return 0;
        }

        return id;
    }


}
